import SelectOptions from "../../async/select-options.vue";
import BaseEditTable from "@rc/pages/components/question-base/base-edit-table.vue";
export default {
    name:'q-compatibility-edit',
    components:{
        SelectOptions,
        BaseEditTable
    },
    props:{
        value: {
            type:Object,
            default:()=>{}
        },
        basicInfo: {
            type: Object,
            default: () => {}
        },
        tagInfo: {
            type: Object,
            default: () => {}
        }
    },
    data(){
        return{
            questionForm:{},
            smallQuestionList:[]
        };
    },
    watch:{
        value:{
            immediate:true,
            deep:true,
            handler(val){
                this.questionForm =  JSON.parse(JSON.stringify(val));
                if(!val.smallQuestionList) {
                    this.smallQuestionList = [];
                    this.$set(this.questionForm,'smallQuestionList',[]);
                } else {
                    this.smallQuestionList = val.smallQuestionList;
                }
                if(!val.smallResourceIds){
                    this.$set(this.questionForm,'smallResourceIds',[]);
                }
                if(!val.optionList || !val.optionList.length) {
                    this.$set(this.questionForm,'optionList',[
                        {optionSeq:'A',answerOption:"",isAnswer:'N',answerSeq:0,key: Date.now()+Math.random()},
                        {optionSeq:'B',answerOption:"",isAnswer:'N',answerSeq:0,key: Date.now()+Math.random()},
                        {optionSeq:'C',answerOption:"",isAnswer:'N',answerSeq:0,key: Date.now()+Math.random()},
                        {optionSeq:'D',answerOption:"",isAnswer:'N',answerSeq:0,key: Date.now()+Math.random()}
                    ]);
                } else {
                    this.questionForm.optionList.forEach((option)=>{
                        option.key = Date.now()+Math.random();
                    });
                }

            }
        },
        questionForm:{
            immediate:true,
            deep: true,
            handler(val){
                this.$emit('change',val);
            }
        }
    },
    methods:{
        checkOption(rule, value, callback) {
            if ( value.answerOption === undefined || value.answerOption === ""){
                return callback(new Error("选项不能为空"));
            }
            const isReceipt = this.questionForm.optionList.some(option=>option.answerOption===value.answerOption && option.optionSeq!==value.optionSeq);
            if( isReceipt) {
                return callback(new Error("选项不能重复"));
            }
            callback();
        },
        /**
         * 添加选项
         */
        add() {
            let optionList = this.questionForm.optionList;
            if (optionList.length > 25) {
				return this.$message.warning("最多可添加26个选项");
			}
			let len = optionList.length + 65;
            let param = {
				optionSeq: String.fromCharCode(len),
				answerOption: "",
                isAnswer:'N',
                answerSeq:0,
                key: Date.now()+Math.random()
			};
			this.questionForm.optionList.push(param);
        },
        /**
         * 删除选项;最后一项显示删除，只保留两个选项
         */
        del(index){
            this.questionForm.optionList.splice(index,1);
        },
        // 编辑
        handleEdit(value,key,index){
            this.$emit('edit',value,key,index);
        },
        subChange(list){
            this.questionForm.smallQuestionList = list;
        },
        //存储被删除的子题id
        deleteSub(deleteResourceIds){
            this.$set(this.questionForm,'smallResourceIds',deleteResourceIds);
        },
        // 校验答案选择
        api_checkAnswer(){
            //校验配伍题小题的答案
            let errorInfo = [];
            this.questionForm.smallQuestionList.forEach((sq,index) => {
                let answerOption = sq.optionList.filter(option=>option.isAnswer==='Y');
                let cond1 = sq.questionType==='S'  && answerOption.length<1;
                let cond2 = (sq.questionType === 'M' ) && answerOption.length<2;
                let cond3 = (sq.questionType === 'I' ) && answerOption.length<1;
                if(cond1 || cond2 ||cond3){
                    errorInfo.push(index+1);
                }
            });
            if(errorInfo.length) {
                let message ='第 '+errorInfo.join(',')+ ' 题缺少正确答案';
                this.$message.warning({
                    dangerouslyUseHTMLString: true,
                    message
                });
            }

            return  !errorInfo.length;
        },
    }
};
